package org.example.demo.controller.userController;

import io.swagger.annotations.ApiOperation;
import org.example.demo.dto.CommentParam;
import org.example.demo.dto.ProductDetail.CommentInfoParam;
import org.example.demo.service.userService.CommentService;
import org.example.mbg.mapper.UserMapper;
import org.example.mbg.model.Comment;
import org.example.mbg.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "/user/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserMapper userMapper;

    @ApiOperation("用户添加评论")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Integer AddToFavorite(@RequestBody CommentParam commentParam) {
        return commentService.add(commentParam);
    }

    @ApiOperation("获得商品评论列表")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public List<CommentInfoParam> get(Long product_id) {
        return commentService.getCommentOfProduct(product_id).stream().map(e -> {
            User u = userMapper.selectByPrimaryKey(e.getUserId());
            CommentInfoParam res = new CommentInfoParam();

            res.setUname(u.getUsername());
            res.setAvatar(u.getAvatar());

            res.setCreated(e.getCreateTime());
            res.setContent(e.getContent());
            res.setStyle("2kg");

            return res;
        }).collect(Collectors.toList());
    }
}
